package com.code.zjm.binder.demo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.code.zjm.binder.demo.R;
import com.code.zjm.binder.demo.entity.Book;
import com.code.zjm.binder.demo.entity.Student;
import com.code.zjm.binder.demo.entity.Teacher;

import java.util.ArrayList;
import java.util.List;

public class ShowTchActivity extends FragmentActivity {
    private ListView lv;
    private List<Teacher> teachers = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_teacher);
        initData();
        initView();

    }

    public void initData() {
        String[] teacherName = {"卢新", "名欧", "祖线"};
        String[] studentName = {"Tom", "Jim", "Mary", "Lus", "Jack", "Lin", "Alice", "Li", "Coco"};
//        String[] bookName = {"Java编程","Android编程","C++编程","php编程","NodeJs服务器编程"};
        List<Book> books = new ArrayList<>();

        books.add(new Book(111, "Java编程"));
        books.add(new Book(112, "Android编程"));
        books.add(new Book(113, "C++编程"));
        books.add(new Book(114, "php编程"));
        books.add(new Book(115, "NodeJs服务器编程"));


        for (int i = 0; i < 3; i++) {
            List<Student> students = new ArrayList<>();
            for (int j = 0; j < 3; j++) {
                int stdIndex = 3 * i + j + 1;
                List<Book> bookList = new ArrayList<>();
                for (int k = 0; k < 3; k++) {
                    bookList.add(books.get(((int) ((Math.random() * books.size())) + k) % books.size()));
                    System.out.println(((int) ((Math.random() * books.size())) + k) % books.size());
                }
                students.add(new Student(Integer.parseInt("1" + stdIndex), studentName[stdIndex - 1], bookList));
            }
            teachers.add(new Teacher(i + 1, teacherName[i], students));

        }

    }

    public void initView() {
        lv = (ListView) findViewById(R.id.lv);
        lv.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, teachers));
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ShowTchActivity.this, ShowStdActivity.class);
                intent.putExtra("studentList", teachers.get(position));
                startActivity(intent);
//                Toast.makeText(ShowTchActivity.this, adapter.getItem(position).getName(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
